package com.visionork.android.twe.model;

import java.io.Serializable;
import java.net.URL;
import java.util.ArrayList;
import java.util.Date;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import android.util.Log;

public class Program implements Serializable, Crawler, Cacheable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private static final String PREFIX_1 = "http://sugoitw.sugoideas.com";
	private static final String PREFIX_2 = "http://sugoideas.com";
	private boolean favorite;
	private String title;
	private String urlLink;
	private String hosts;
	private Date releaseDate;
	private ArrayList<Episode> episodes;
	private boolean refreshFlag;

	public Program() {
		episodes = new ArrayList<Episode>();
	}

	public Episode getEpisode(String key) {
		for (Episode episode : episodes) {
			if (episode.getKey().equals(key)) {
				return episode;
			}
		}
		return null;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getTitle() {
		return title;
	}

	@Override
	public void Crawl() {
		ArrayList<Episode> tmpEpisodes = new ArrayList<Episode>();
		try {
			Document doc = Jsoup.parse(new URL(urlLink), 5000);
			Elements elms = doc.getElementsByTag("a");

			for (Element elm : elms) {
				Log.i("GOLDB", elm.attr("href"));
				String href = elm.attr("href");
				if (elm.hasAttr("target")
						&& elm.attr("target").equals("_blank")
						&& elm.hasText()
						&& (href.startsWith(PREFIX_1) || href
								.startsWith(PREFIX_2))) {
					String title = elm.text();
					if (title != null && title.trim().length() > 0) {
						Episode episode = new Episode();
						episode.setTitle(title);
						episode.setUrlLink(href);
						tmpEpisodes.add(episode);
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		for (Episode episode : tmpEpisodes) {
			if (!episodes.contains(episode)) {
				/* add new one */
				episodes.add(episode);
			} else {
				/* update old one */
				Episode existEpisode = episodes.get(episodes.indexOf(episode));
				existEpisode.setTitle(episode.getTitle());
			}
		}

		/* remove expired one */
		for (Episode episdoe : episodes) {
			if (!tmpEpisodes.contains(episdoe)) {
				episodes.remove(episdoe);

			}
		}
	}

	public void setUrlLink(String urlLink) {
		this.urlLink = urlLink;
	}

	public String getUrlLink() {
		return urlLink;
	}

	@Override
	public String toString() {
		return String.format("title = '%s', urlLink = '%s'", title, urlLink);
	}

	public void setEpisodes(ArrayList<Episode> episodes) {
		this.episodes = episodes;
	}

	public synchronized ArrayList<Episode> getEpisodes() {
		if (episodes == null || episodes.size() == 0 || refreshFlag) {
			Crawl();
		}

		if (refreshFlag) {
			refreshFlag = false;
		}
		return episodes;
	}

	@Override
	public String getKey() {
		return urlLink;
	}

	@Override
	public boolean equals(Object obj) {
		return getKey().equals(((Program) obj).getKey());
	}

	public void setHosts(String hosts) {
		this.hosts = hosts;
	}

	public String getHosts() {
		return hosts;
	}

	public void setReleaseDate(Date releaseDate) {
		this.releaseDate = releaseDate;
	}

	public Date getReleaseDate() {
		return releaseDate;
	}

	public void refresh() {
		refreshFlag = true;
	}

	public void setFavorite(boolean favorite) {
		this.favorite = favorite;
	}

	public boolean isFavorite() {
		return favorite;
	}
}
